package com.tchintech.pay.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.model.entity.Generalize;
import com.tchintech.pay.utils.AppConst;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/8/18 0018.
 */

public class GeneralizeAdapter extends BaseAdapter {

    private Context context;
    private List<Generalize> list;

    public GeneralizeAdapter(Context context, List<Generalize> list) {
        this.context = context;
        this.list = list;
    }

    public void setList(List<Generalize> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Generalize getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View v, ViewGroup parent) {
        ViewHolde vh = null;
        if (v == null) {
            vh = new ViewHolde();
            v = LayoutInflater.from(context).inflate(R.layout.item_generalize, null);
            vh.iv_class = (ImageView) v.findViewById(R.id.iv_class);
            vh.tv_class = (TextView) v.findViewById(R.id.tv_class);

            vh.tv_quick = (TextView) v.findViewById(R.id.tv_quick);
            vh.tv_wx = (TextView) v.findViewById(R.id.tv_wx);
            vh.tv_zfb = (TextView) v.findViewById(R.id.tv_zfb);
            vh.tv_sj = (TextView) v.findViewById(R.id.tv_sj);
            v.setTag(vh);
        } else {
            vh = (ViewHolde) v.getTag();
        }
        final Generalize generalize = list.get(position);
        vh.tv_quick.setText(generalize.getQuick());
        vh.tv_wx.setText(generalize.getWx());
        vh.tv_zfb.setText(generalize.getZfb());
        if (generalize.getGrade() == 1) {
            vh.iv_class.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.iv_silver));
            vh.tv_class.setText("白银会员");
        } else if (generalize.getGrade() == 2) {
            vh.iv_class.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.iv_gold));
            vh.tv_class.setText("黄金会员");
        } else if (generalize.getGrade() == 3) {
            vh.iv_class.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.iv_platina));
            vh.tv_class.setText("白金会员");
        } else if (generalize.getGrade() == 4) {
            vh.tv_class.setText("钻石会员");
            vh.iv_class.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.iv_jewel));
        }
        vh.tv_class.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        vh.tv_class.getPaint().setAntiAlias(true);//抗锯齿
        //获取用户当前等级，只让大于他的等级可以升级
        int grades = Integer.parseInt(AppConst.getUser().getGradesid());
        if (position + 1 > grades) {
            vh.tv_sj.setVisibility(View.VISIBLE);
        } else {
            vh.tv_sj.setVisibility(View.GONE);
        }
        vh.tv_sj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemDataClick.OnItemDataClick(generalize);
            }
        });
        return v;
    }

    class ViewHolde {
        ImageView iv_class;
        TextView tv_class, tv_quick, tv_wx, tv_zfb, tv_sj;
    }

    public interface OnitemDataClick {
        void OnItemDataClick(Generalize generalize);
    }

    private OnitemDataClick onitemDataClick;

    public void setOnitemDataClick(OnitemDataClick onitemDataClick) {
        this.onitemDataClick = onitemDataClick;

    }
}
